# encoding: utf-8

# 常用跳转性函数的集合

# 进入通知页面
def go_notice
  @driver.get(@base_url + 'system_notifications')
end

# 进入wiki主页、git库、历史、页面
def go_in_wiki_page(user, project, type = '')
  username = get_hash(user, 'username')
  project_name = get_hash(project, 'project_name')

  page = '/wikis'

  case type
    when 'git'
      page += '/_git_repo'
    when 'history'
      page += '/_history'
    when 'pages'
      page += '/_pages'
  end

  @driver.get(@base_url+username+'/'+project_name+page)
end

# 进入首页
def go_home
  @driver.get(@base_url)
end

# 进入默认项目首页
def go_info_project_index(username, project_name = $projects['default'])
  go_project_subpage(username, project_name)
end

# 进入某项目首页
def go_project_index(user, project)
  go_project_subpage(user, project)
end

# 进入项目首页下面的子页面
def go_project_subpage(user, project, subpage = 'default')
  username = (user.class == Hash ? user['username']: user)
  project_name = (project.class == Hash ? project['project_name']: project)

  page = '/'
  case subpage
  when 'events'
    page = page + 'events'
  when 'users'
    page = page + 'team_members'
  end

  @driver.get(@base_url+username+'/'+project_name+page)
end

# 进入项目代码页
def go_code_subpage(user, project, subpage = 'default')
  username = (user.class == Hash ? user['username']: user)
  project_name = (project.class == Hash ? project['project_name']: project)

  page = '/'
  case subpage
    when 'history'
    page = page + 'commits/master'
    when 'branch'
      page = page + 'repository/branches'
    when 'tag'
      page = page + 'repository/tags'
    when 'compare'
      page = page + 'compare'
    else
      page = page + 'tree/master'
  end

  @driver.get(@base_url+username+'/'+project_name+page)
end

# 进入项目设置相关页面
def go_project_set(user, project, type = 'default')
  username = (user.class == Hash ? user['username']: user)
  project_name = (project.class == Hash ? project['project_name']: project)
  page = '/'
  case type
    # 项目成员
    when 'member'
      page = page + 'settings/team_members'
    #  功能管理
    when 'manage'
      page = page + 'feature_manage'
    #  项目拱腰
    when 'key'
      page = page + 'deploy_keys'
    #  项目设置
    else
      page = page + 'edit'
  end

  @driver.get(@base_url+username+'/'+project_name+page)
end

# 用户页面相关 start

# 进入某用户主页
def go_user_index(user)
  username = user['username']
  @driver.get(@base_url+username)
end

# 进入账户设置页面
def go_profile_subpage(subpage = 'default')
  page = '/'

  case subpage
    when 'key'
      page = page + 'keys'
    when 'notify'
      page = page + 'profile/notify_setting'
    when 'log'
      page = page + 'profile/history'
    else
      page = page + 'profile'
  end
  @driver.get(@base_url+page)
end